﻿package mge.ui.controls 
{
  import flash.display.MovieClip
  import flash.events.Event;
  import flash.utils.getDefinitionByName;
  import mge.ui.events.ButtonEvent;
  import mge.ui.events.ListEvent;
  
	/**
   * ...
   * @author ivan
   */
  public class List extends Button
  {
    
    [Inspectable(defaultValue="")]
    public function get items():Array { return _items; }
    public function set items(value:Array):void { trace("items set");  _items = value; }
    
    
    [Inspectable(name="itemRenderer", defaultValue="SkinnedListItemRenderer")]
    public function set itemRendererName(value:String):void 
    {       
      var classRef:Class = getDefinitionByName(value) as Class;
      if (classRef != null) {
          itemRenderer = classRef;
      } else {
          trace("Error: " + this + ", The class " + value + " cannot be found in your library. Please ensure it is there.");
      }
    }
    
    
    
    private var _items:Array;    
    
    private var itemRenderer:Class;
    private var renderers:Vector.<mge.ui.controls.ListItemRenderer>;
    
    public function List() 
    {
      super();
      
      if (loaderInfo)
        loaderInfo.addEventListener(Event.INIT, onInit);
      else
      {
        addEventListener(Event.ENTER_FRAME, onInit);
        // onInit();
      }
    }
    
    private function onInit(e:Event = null):void
    {
      trace("List init");
      removeEventListener(Event.ENTER_FRAME, onInit);
      if (loaderInfo)
        loaderInfo.removeEventListener(Event.INIT, onInit);
              
      renderers = new Vector.<mge.ui.controls.ListItemRenderer>();
      
      var rowHeight:Number = 20;
      var margin:Number = 5;
      // Width and height of list is setted here.
      height = _items.length * rowHeight + 1;
      
      rowHeight /= scaleY;
      
      for (var i:int = 0; i < _items.length; i++)
      {
        var r:mge.ui.controls.ListItemRenderer = new itemRenderer();
        r.data = _items[i];
        renderers.push(r);
        r.height = rowHeight;
        r.x = margin;
        r.y = rowHeight * i;
        r.addEventListener(ButtonEvent.CLICK, onItemClick);
        addChild(r);
      }
      
      if (renderers.length > 0)
      {
        selectedButton = renderers[0];
        selectedButton.selected = true;
      }
      
      buttonMode = false;
            
      draw();
    }
    
    private function onItemClick(e:ButtonEvent):void
    {
      selectedButton.selected = false;
      selectedButton = e.target as Button;
      selectedButton.selected = true;
      
      var i:int = 0;
      for (; i < renderers.length; i++)
        if (selectedButton == renderers[i]) break;
      
      var event:ListEvent = new ListEvent(ListEvent.ITEM_SELECTED);
      event.selectedIndex = i;
      event.selectedItem = selectedButton;
      dispatchEvent(event);
    }
    
    private function draw():void
    {
      trace(_items);
    }
    
    private var selectedButton:Button;
    
  }

}